/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "DX纹理缓存.h"



void f_取纹理采样级别(ID3D12Device* dev, S_纹理创建参数& 参数) {
	D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS 采样质量级别 = {};
	采样质量级别.Format = (DXGI_FORMAT)参数.format;
	采样质量级别.SampleCount = 参数.sample;
	采样质量级别.Flags = D3D12_MULTISAMPLE_QUALITY_LEVELS_FLAG_NONE;
	采样质量级别.NumQualityLevels = 0;

	dev->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &采样质量级别, sizeof(采样质量级别));
	参数.sampleQuality = 采样质量级别.NumQualityLevels;
}


/*explicit f_D3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE type, uint32 creationNodeMask = 1, uint32 nodeMaks=1) {
	Type = type;
	CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
	MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
	CreationNodeMask = creationNodeMask;
	VisibleNodeMask = nodeMaks;
}*/




S_DX纹理缓存::S_DX纹理缓存()
{

}

uint32 S_DX纹理缓存::f_创建内存对象(const void* c, void* ctx, uint32 key)
{
	ID3D12Device* dev = (ID3D12Device*)ctx;
	D3D12_RESOURCE_DESC GPU资源 = {};


	const S_纹理创建参数& 参数 = *((const S_纹理创建参数*)c);

	
	if (参数.size.z <= 1) {
		GPU资源.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
	}



	GPU资源.Alignment = 0;
	GPU资源.Width = 参数.size.x;
	GPU资源.Height = 参数.size.y;
	GPU资源.DepthOrArraySize = 参数.size.z;
	GPU资源.MipLevels = 参数.mip;
	GPU资源.Format = (DXGI_FORMAT)参数.format;
	GPU资源.SampleDesc.Count = 参数.sample;
	GPU资源.SampleDesc.Quality = D3D12_TEXTURE_LAYOUT_UNKNOWN;
	GPU资源.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
	GPU资源.Flags = (D3D12_RESOURCE_FLAGS)参数.type;


	D3D12_CLEAR_VALUE 清除值 = {};
	清除值.Format = GPU资源.Format;
	清除值.DepthStencil.Depth = 1.0f;
	清除值.DepthStencil.Stencil = 0;

	D3D12_HEAP_PROPERTIES 堆性能 = { D3D12_HEAP_TYPE_DEFAULT };
	堆性能.Type = D3D12_HEAP_TYPE_DEFAULT;
	堆性能.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
	堆性能.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
	堆性能.CreationNodeMask = 1;
	堆性能.VisibleNodeMask = 1;

	dev->CreateCommittedResource(
		&堆性能,
		D3D12_HEAP_FLAG_NONE,
		&GPU资源,
		D3D12_RESOURCE_STATE_COMMON,
		&清除值,
		IID_PPV_ARGS(m_Data.GetAddressOf())
	);


	return 0;
}

void S_DX纹理缓存::f_Fill(void * data, int3_64 size, void * ctx)
{

}

void * S_DX纹理缓存::f_map()
{
	return nullptr;
}

void S_DX纹理缓存::f_unmap()
{
}



S_DX纹理缓存 * f_创建深度模板纹理(ID3D12Device* dev, ivec2& size, uint32 采样, uint32 key)
{
	S_纹理创建参数 深度模板创建参数 = { 0 };

	深度模板创建参数.size.x = size.x;
	深度模板创建参数.size.y = size.y;
	深度模板创建参数.size.z = 1;

	深度模板创建参数.format = DXGI_FORMAT::DXGI_FORMAT_R32_FLOAT;
	深度模板创建参数.type = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
	深度模板创建参数.mip = 1;
	深度模板创建参数.sample = 采样;
	f_取纹理采样级别(dev, 深度模板创建参数);
	深度模板创建参数.dim;


	S_DX纹理缓存* 深度模板 = new S_DX纹理缓存();

	深度模板->f_创建内存对象(&深度模板创建参数, 0);


	//D3D12_CPU_DESCRIPTOR_HANDLE;

	//dev->CreateDepthStencilView();

	return nullptr;
}




